package com.hnote.hnoteapplication.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.View;
import android.view.ViewGroup;

import com.hnote.hnoteapplication.BR;
import com.hnote.hnoteapplication.R;
import com.hnote.hnoteapplication.base.BaseQuickAdapter;
import com.hnote.hnoteapplication.base.BaseViewHolder;
import com.hnote.hnoteapplication.entity.MenuItem;

import java.util.List;


/**
 * 描述：
 * 作者：HNote
 * 邮箱：liupengfei930311@163.com
 * 日期：2017-03-08 下午8:40
 **/
public class MenuAdapter extends BaseQuickAdapter<MenuItem, MenuAdapter.MenuViewHolder> {

    public MenuAdapter(int layoutResId, List<MenuItem> data){
        super(layoutResId, data);
    }

    @Override
    protected void convert(MenuAdapter.MenuViewHolder helper, MenuItem item) {
        ViewDataBinding binding = helper.getBinding();
        binding.setVariable(BR.model, item);
        binding.executePendingBindings();
    }

    @Override
    protected MenuAdapter.MenuViewHolder createBaseViewHolder(View view) {
        return new MenuAdapter.MenuViewHolder(view);
    }

    @Override
    protected View getItemView(int layoutResId, ViewGroup parent) {
        ViewDataBinding binding = DataBindingUtil.inflate(mLayoutInflater, layoutResId, parent, false);
        if (binding == null) {
            return super.getItemView(layoutResId, parent);
        }
        View view = binding.getRoot();
        view.setTag(R.id.BaseQuickAdapter_databinding_support, binding);
        return view;
    }

    public class MenuViewHolder extends BaseViewHolder {

        public MenuViewHolder(View view) {
            super(view);
        }

        public ViewDataBinding getBinding() {
            return (ViewDataBinding) getConvertView().getTag(R.id.BaseQuickAdapter_databinding_support);
        }
    }
}
